package com.hubu.web;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.hubu.mapper.administratorUserMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

@WebServlet("/deleteStudent")
public class deleteStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String studentId = request.getParameter("studentId");
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        administratorUserMapper administratorUserMapper=sqlSession.getMapper(com.hubu.mapper.administratorUserMapper.class);
        administratorUserMapper.deleteStudentById(studentId);

        sqlSession.commit();
        sqlSession.close();

        PrintWriter writer=response.getWriter();
        writer.println("<script type='text/javascript'>");
        writer.println("alert('删除成功！');");
        writer.println("window.location='" + request.getContextPath() + "/adminHome';");
        writer.println("</script>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
